<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
            <el-form-item label="货品名称" prop="productId">
                <el-select v-model="queryParams.productId" placeholder="请选择货品名称" clearable size="small"
                @change="handleQuery">
                    <el-option v-for="item in products" :key="item.id" :label="item.productName" :value="item.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="仓库名称" prop="warehouseId">
                <el-select v-model="queryParams.warehouseId" placeholder="请选择仓库名称" clearable size="small"
                    @change="handleQuery">
                    <el-option v-for="item in wareHouses" :key="item.id" :label="item.warehouseName"
                    :value="item.id"></el-option>
                </el-select>
            </el-form-item>
            <!-- <el-form-item label="库存数量" prop="num">
        <el-input
          v-model="queryParams.num"
          placeholder="请输入库存数量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:storage:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:storage:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:storage:remove']"
        >删除</el-button>
      </el-col> -->
            <el-col :span="1.5">
                <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['system:storage:export']">导出</el-button>
            </el-col>
<!--            <el-col :span="1.5">-->
<!--                <el-button type="warning" icon="el-icon-download" size="mini" @click="handleOutInExport"-->
<!--                    v-hasPermi="['system:storage:export']">货品出入库报表</el-button>-->
<!--            </el-col>-->
        </el-row>

        <el-table v-loading="loading" :data="storageList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <!-- <el-table-column
        label="主键id"
        align="center"
        prop="id"
      /> -->
            <el-table-column label="仓库名称" align="center" prop="warehouseName" />
            <el-table-column label="名称" align="center" prop="productName" />
            <el-table-column label="单价" align="center" prop="price" />

            <el-table-column label="损耗数量" align="center" prop="lossNumber" />
            <el-table-column label="损耗备注" align="center" prop="lossRemark" />

          <el-table-column label="库存数量" align="center" prop="num" />

            <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:storage:edit']"
          >修改</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['system:storage:remove']"-->
<!--          >删除</el-button>-->
        </template>
      </el-table-column>

        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />

        <!-- 添加或修改货品存储信息对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="仓库名称" prop="warehouseId">
                    <el-select v-model="form.warehouseId" placeholder="请选择仓库名称" clearable size="small"
                        @change="handleQuery">
                        <el-option v-for="item in wareHouses" :key="item.id" :label="item.warehouseName"
                            :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="货品名称" prop="productId">
                    <el-select v-model="form.productId" placeholder="请选择货品名称" clearable size="small"
                        @change="handleQuery">
                        <el-option v-for="item in products" :key="item.id" :label="item.productName"
                            :value="item.id"></el-option>
                    </el-select>
                </el-form-item>

              <el-form-item label="库存数量" prop="num" >
                <el-input-number v-model="form.num" placeholder="请选择库存数量" :min="0" />
              </el-form-item>

              <el-form-item label="损耗数量" prop="lossNumber" >
                <el-input-number v-model="form.lossNumber" placeholder="请选择损耗数量" :min="0" :max="Number(form.num)" />
              </el-form-item>

              <el-form-item label="损耗备注" >
                <el-input type="textarea" placeholder="请输入损耗备注" v-model="form.lossRemark"></el-input>
              </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
    listStorage,
    getStorage,
    delStorage,
    addStorage,
    updateStorage,
    exportStorage,
    exportOutInStorage
} from "@/api/system/storage";
import { getWarehouses } from "@/api/warehouse/warehouse";
import { getProducts } from "@/api/productadmin/productadmin";

export default {
    name: "Storage",
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 总条数
            total: 0,
            // 货品存储信息表格数据
            storageList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                productId: undefined,
                warehouseId: undefined,
                num: undefined,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
              warehouseId: [
                {required: true, message: '请选择仓库名称', trigger: 'change'}
              ],
              productId: [
                {required: true, message: '请选择货品名称', trigger: 'change'}
              ],
              lossNumber: [
                {required: true, message: '请选择损耗数量', trigger: 'change'}
              ],
              lossRemark: [
                {required: true, message: '请输入损耗备注', trigger: 'change'}
              ],
              num: [
                {required: true, message: '请选择库存数量', trigger: 'change'}
              ],
            },
            // 仓库参数
            wareHouses: [],
            // 货品参数
            products: [],
        };
    },
    created() {
        this.getList();
        this.getWarehouses();
        this.getProducts();
    },
    methods: {
        /** 获取仓库列表 */
        getWarehouses() {
            getWarehouses().then((response) => {
                this.wareHouses = response.data;
            });
        },

        /** 获取货品列表 */
        getProducts() {
            getProducts().then((response) => {
                this.products = response.data;
            });
        },

        /** 查询货品存储信息列表 */
        getList() {
            this.loading = true;
            listStorage(this.queryParams).then((response) => {
                this.storageList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: undefined,
                productId: undefined,
                warehouseId: undefined,
                num: undefined,
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id);
            this.single = selection.length != 1;
            this.multiple = !selection.length;
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加货品存储信息";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids;
            getStorage(id).then((response) => {
                this.form = response.data;
              this.form.warehouseId =Number(response.data.warehouseId)
              this.form.productId=Number(response.data.productId)
              this.open = true;
                this.title = "修改货品存储信息";
            });
        },
        /** 提交按钮 */
        submitForm: function () {
            this.$refs["form"].validate((valid) => {
                if (valid) {
                    if (this.form.id != undefined) {
                        // updateStorage(this.form).then((response) => {
                        //     if (response.code === 200) {
                        //         this.msgSuccess("修改成功");
                        //         this.open = false;
                        //         this.getList();
                        //     }
                        // });
                      updateStorage(this.form).then((response) => {
                        this.msgSuccess("修改成功");
                        this.open = false;
                        this.getList();
                      });
                    } else {
                        addStorage(this.form).then((response) => {
                            if (response.code === 200) {
                                this.msgSuccess("新增成功");
                                this.open = false;
                                this.getList();
                            }
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$confirm(
                '是否确认删除货品存储信息编号为"' + ids + '"的数据项?',
                "警告",
                {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                }
            )
                .then(function () {
                    return delStorage(ids);
                })
                .then(() => {
                    this.getList();
                    this.msgSuccess("删除成功");
                })
                .catch(function () { });
        },
        handleOutInExport() {
            const queryParams = this.queryParams;
            this.$confirm("是否确认导出所有货品存储信息数据项?", "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(function () {
                    return exportOutInStorage(queryParams);
                })
                .then((response) => {
                    this.download(response.data);
                })
                .catch(function () { });
        },
        /** 导出按钮操作 */
        handleExport() {
            const queryParams = this.queryParams;
            this.$confirm("是否确认导出所有货品存储信息数据项?", "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(function () {
                    return exportStorage(queryParams);
                })
                .then((response) => {
                    this.download(response.data);
                })
                .catch(function () { });
        },
    },
};
</script>
